[% setvar title A method of allowing foreign objects in perl %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>A method of allowing foreign objects in perl</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Dan Sugalski &lt;<a href='mailto:dan@sidhe.org'>dan@sidhe.org</a>&gt;
  Date: 2 Aug 2000
  Mailing List: <a href='mailto:perl6-internals@perl.org'>perl6-internals@perl.org</a>
  Number: 32
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Currently it's a darned pain to deal with objects created in other
languages in perl. It would be very nice to be able to call a function
written in some other language, take the returned object, and treat it
as a perl object. (Which means making method calls and such on it)</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Everybody and their dog has written an object-oriented language at
this point, and it'd be nice to be able to use objects created by
those languages as if they were real perl objects. This would make
interfacing perl and OO languages[1] easier, and make things much
nicer for embedded and embedding code.</p>
<p>Since writing half a zillion perl2WhoKnowsWhat modules is going to be
unweildy, not to mention different for each and every object type,
perl punts. We require that the code we interface to defines a
dispatch function for each object, and we then just call the dispatch
function with enough information for it to Do The Right Thing.</p>
<p>&gt;From the perl programmer's point of view, foreign objects and native
objects look the same. Perl will define some generic conversion
routines (to int, float, and string) which the foreign code is welcome
to override if it wishes.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>A scalar is made an object via a call into the perl library. The
scalar is marked as an object and stuck into a package. Attached to
the scalar is a pointer to the native object, a pointer to a generic
dispatch routine, and a pointer to the native destruction routine for
the object.</p>
<p>Native code would create the object thusly:</p>
<pre>  TheObj *foo;
  SV *new_sv;
  foo = new TheObj(&quot;A parameter&quot;);
  sv = perl_new_sv();
  perl_make_sv_object(sv, &quot;Some::Package&quot;, foo, &amp;dispatch_routine,
			                        &amp;destroy_routine);

  perl_return(perl_make_ref(sv));</pre>
<p>When you make a method call in perl, like so:</p>
<pre>  $native_obj-&gt;foo($bar, $baz);</pre>
<p>perl calls the underlying dispatch routine to do the actual work. The
dispatch routine has a function signature like so:</p>
<pre>  int status = dispatch(void *native_obj, sv *perl_scalar, char *method_called,
	                int *num_args_in, perl_arg_stack *arg_stack,
	                int *num_args_out, perl_arg_stack *return_values);</pre>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>None I can think of</p>
<a name='FOOTNOTES'></a><h1>FOOTNOTES</h1>
<p>[1] Like, say, C++ or Scheme</p>
</div>
